import UIKit

struct Message {

    static var tableName: String = "messages"

    var messageId: String
    var conversationId: String
    var userId: String
    var category: String
    var content: String? = nil
    var mediaUrl: String? = nil
    var mediaMimeType: String? = nil
    var mediaSize: Int64? = nil
    var mediaDuration: Int64? = nil
    var mediaWidth: Int? = nil
    var mediaHeight: Int? = nil
    var mediaHash: String? = nil
    var mediaKey: Data? = nil
    var mediaDigest: Data? = nil
    var mediaStatus: String? = nil
    var mediaWaveform: Data? = nil
    var thumbImage: String? = nil
    var status: String
    var action: String? = nil
    var participantId: String? = nil
    var snapshotId: String? = nil
    var name: String? = nil
    var stickerId: String? = nil
    var sharedUserId: String? = nil
    var quoteMessageId: String? = nil
    var quoteContent: Data? = nil
    var createdAt: String
}

enum MessageCategory: String {
    case SIGNAL_KEY
    case SIGNAL_TEXT
    case SIGNAL_IMAGE
    case SIGNAL_VIDEO
    case SIGNAL_DATA
    case SIGNAL_STICKER
    case SIGNAL_CONTACT
    case SIGNAL_AUDIO
    case PLAIN_TEXT
    case PLAIN_IMAGE
    case PLAIN_VIDEO
    case PLAIN_DATA
    case PLAIN_STICKER
    case PLAIN_CONTACT
    case PLAIN_JSON
    case PLAIN_AUDIO
    case APP_CARD
    case APP_BUTTON_GROUP
    case SYSTEM_CONVERSATION
    case SYSTEM_ACCOUNT_SNAPSHOT
    case EXT_UNREAD
    case EXT_ENCRYPTION
    case UNKNOWN
    
    static let maxIconSize: CGSize = {
        
        let maxLength = max(#imageLiteral(resourceName: "ic_message_photo").size.width, #imageLiteral(resourceName: "ic_message_photo").size.height,
                            #imageLiteral(resourceName: "ic_message_sticker").size.width, #imageLiteral(resourceName: "ic_message_sticker").size.height,
                            #imageLiteral(resourceName: "ic_message_contact").size.width, #imageLiteral(resourceName: "ic_message_contact").size.height,
                            #imageLiteral(resourceName: "ic_message_file").size.width, #imageLiteral(resourceName: "ic_message_file").size.height,
                            #imageLiteral(resourceName: "ic_message_video").size.width, #imageLiteral(resourceName: "ic_message_video").size.height,
                            #imageLiteral(resourceName: "ic_message_audio").size.width, #imageLiteral(resourceName: "ic_message_audio").size.height,
                            #imageLiteral(resourceName: "ic_message_transfer").size.width, #imageLiteral(resourceName: "ic_message_transfer").size.height,
                            #imageLiteral(resourceName: "ic_message_bot_menu").size.width, #imageLiteral(resourceName: "ic_message_bot_menu").size.height)
        return CGSize(width: maxLength, height: maxLength)
    }()
    
    static func iconImage(forMessageCategoryString category: String) -> UIImage? {
        if category.hasSuffix("_IMAGE") {
            return #imageLiteral(resourceName: "ic_message_photo")
        } else if category.hasSuffix("_STICKER") {
            return #imageLiteral(resourceName: "ic_message_sticker")
        } else if category.hasSuffix("_CONTACT") {
            return #imageLiteral(resourceName: "ic_message_contact")
        } else if category.hasSuffix("_DATA") {
            return #imageLiteral(resourceName: "ic_message_file")
        } else if category.hasSuffix("_VIDEO") {
            return #imageLiteral(resourceName: "ic_message_video")
        } else if category.hasSuffix("_AUDIO") {
            return #imageLiteral(resourceName: "ic_message_audio")
        } else if category == MessageCategory.SYSTEM_ACCOUNT_SNAPSHOT.rawValue {
            return #imageLiteral(resourceName: "ic_message_transfer")
        } else if category == MessageCategory.APP_BUTTON_GROUP.rawValue || category == MessageCategory.APP_CARD.rawValue {
            return #imageLiteral(resourceName: "ic_message_bot_menu")
        } else {
            return nil
        }
    }
}

enum MessageStatus: String, Codable {
    case SENDING
    case SENT
    case DELIVERED
    case READ
    case UNKNOWN
    case FAILED

    static func getOrder(messageStatus: String) -> Int {
        switch messageStatus {
        case MessageStatus.SENDING.rawValue:
            return 0
        case MessageStatus.SENT.rawValue:
            return 1
        case MessageStatus.FAILED.rawValue:
            return 2
        case MessageStatus.DELIVERED.rawValue:
            return 3
        case MessageStatus.READ.rawValue:
            return 4
        default:
            return -1
        }
    }
}

enum MediaStatus: String {
    case PENDING
    case DONE
    case CANCELED
    case EXPIRED
}
